/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inputsms.utilities;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import inputsms.model.Content;
import org.apache.log4j.Logger;

/**
 *
 * @author aisyah
 */
public class MapKey {

    private static Logger logger = Logger.getLogger(MapKey.class);
    private String smsContent;
    private String optr;
    private String keyDiagnostic;

    public MapKey(String smsContent, String optr) {
        this.smsContent = smsContent;
        this.optr = optr;
        this.keyDiagnostic = smsContent.substring(0, (smsContent.length() > DefaultKey.MAX_CHAR_KEY) ? DefaultKey.MAX_CHAR_KEY : smsContent.length()).toUpperCase();

    }

    public Content getContent() {
        Content co = new Content();
        Map map = mapingMap(optr);
        if (keyDiagnostic.contains(Property.getPropOption(optr, "fb.stop"))) {
            co.setKey(map.get(Property.getPropOption(optr, "fb.stop")).toString());
            //check isi content selain keyword
            if (smsContent.length() > Property.getPropOption(optr, "fb.stop").length()) {
                co.setIsi(smsContent.substring(Property.getPropOption(optr, "fb.stop").length() + 1, smsContent.length()));
            }
        } else if (keyDiagnostic.contains(Property.getPropOption(optr, "fb.login"))) {
            co.setKey(map.get(Property.getPropOption(optr, "fb.login")).toString());
            //check isi content selain keyword
            if (smsContent.length() > Property.getPropOption(optr, "fb.login").length()) {
                co.setIsi(smsContent.substring(Property.getPropOption(optr, "fb.login").length() + 1, smsContent.length()));
            }
        } else if (keyDiagnostic.contains(Property.getPropOption(optr, "fb.buddy"))) {
            co.setKey(map.get(Property.getPropOption(optr, "fb.buddy")).toString());
            //check isi content selain keyword
            if (smsContent.length() > Property.getPropOption(optr, "fb.buddy").length()) {
                co.setIsi(smsContent.substring(Property.getPropOption(optr, "fb.buddy").length() + 1, smsContent.length()));
            }
        } else if (keyDiagnostic.contains(Property.getPropOption(optr, "fb.buddynew"))) {
            co.setKey(map.get(Property.getPropOption(optr, "fb.buddynew")).toString());
            //check isi content selain keyword
            if (smsContent.length() > Property.getPropOption(optr, "fb.buddynew").length()) {
                co.setIsi(smsContent.substring(Property.getPropOption(optr, "fb.buddynew").length() + 1, smsContent.length()));
            }
        } else if (keyDiagnostic.contains(Property.getPropOption(optr, "fb.to"))) {
            co.setKey(map.get(Property.getPropOption(optr, "fb.to")).toString());
            //check isi content selain keyword
            if (smsContent.length() > Property.getPropOption(optr, "fb.to").length()) {
                co.setIsi(smsContent.substring(Property.getPropOption(optr, "fb.to").length() + 1, smsContent.length()));
            }
        } else if (keyDiagnostic.contains(Property.getPropOption(optr, "fb.logout"))) {
            co.setKey(map.get(Property.getPropOption(optr, "fb.logout")).toString());
            //check isi content selain keyword
            if (smsContent.length() > Property.getPropOption(optr, "fb.logout").length()) {
                co.setIsi(smsContent.substring(Property.getPropOption(optr, "fb.logout").length() + 1, smsContent.length()));
            }
        } else if (keyDiagnostic.contains(Property.getPropOption(optr, "reg.fb"))) {
            co.setKey(map.get(Property.getPropOption(optr, "reg.fb")).toString());
            //check isi content selain keyword
            if (smsContent.length() > Property.getPropOption(optr, "reg.fb").length()) {
                co.setIsi(smsContent.substring(Property.getPropOption(optr, "reg.fb").length() + 1, smsContent.length()));
            }
        } else if (keyDiagnostic.contains(Property.getPropOption(optr, "fb.help.more"))) {
            co.setKey(map.get(Property.getPropOption(optr, "fb.help.more")).toString());
            //check isi content selain keyword
            if (smsContent.length() > Property.getPropOption(optr, "fb.help.more").length()) {
                co.setIsi(smsContent.substring(Property.getPropOption(optr, "fb.help.more").length() + 1, smsContent.length()));
            }
        } else if (keyDiagnostic.contains(Property.getPropOption(optr, "fb.help"))) {
            co.setKey(map.get(Property.getPropOption(optr, "fb.help")).toString());
            //check isi content selain keyword
            if (smsContent.length() > Property.getPropOption(optr, "fb.help").length()) {
                co.setIsi(smsContent.substring(Property.getPropOption(optr, "fb.help").length() + 1, smsContent.length()));
            }
        } else if (smsContent.contains(Property.getPropOption(optr, "fb.change"))) {
            co.setKey(map.get(Property.getPropOption(optr, "fb.change")).toString());
            //check isi content selain keyword
            if (smsContent.length() > Property.getPropOption(optr, "fb.change").length()) {
                co.setIsi(smsContent.substring(Property.getPropOption(optr, "fb.change").length() + 1, smsContent.length()));
            }
        } else if (keyDiagnostic.contains(Property.getPropOption(optr, "fb.status"))) {
            co.setKey(map.get(Property.getPropOption(optr, "fb.status")).toString());
            //check isi content selain keyword
            if (smsContent.length() > Property.getPropOption(optr, "fb.status").length()) {
                co.setIsi(smsContent.substring(Property.getPropOption(optr, "fb.status").length() + 1, smsContent.length()));
            }
        } else {
            //detected keyword wrong
            co.setKey(Property.getProperty("dir.fault"));
            co.setIsi(MessageId.COMMAND_NOT_FOUND);
            logger.error(Property.getProperty("msg.failed.keyword"));
        }

        return co;
    }

    public Map<String, String> mapingMap(String optr) {
        Map<String, String> mp = new HashMap<String, String>();
        // adding or set elements in Map by put method key and value pair
        mp.put(Property.getPropOption(optr, "reg.fb"), DefaultKey.REG_FB);
        mp.put(Property.getPropOption(optr, "fb.login"), DefaultKey.FB_LOGIN);
        mp.put(Property.getPropOption(optr, "fb.buddy"), DefaultKey.FB_BUDDY);
        mp.put(Property.getPropOption(optr, "fb.buddynew"), DefaultKey.FB_BUDYNEW);
        mp.put(Property.getPropOption(optr, "fb.to"), DefaultKey.FB_TO);
        mp.put(Property.getPropOption(optr, "fb.logout"), DefaultKey.FB_LOGOUT);
        mp.put(Property.getPropOption(optr, "fb.stop"), DefaultKey.FB_STOP);
        mp.put(Property.getPropOption(optr, "fb.help"), DefaultKey.FB_HELP);
        mp.put(Property.getPropOption(optr, "fb.help.more"), DefaultKey.FB_HELP_MORE);
        mp.put(Property.getPropOption(optr, "fb.change"), DefaultKey.FB_CHANGE);
        mp.put(Property.getPropOption(optr, "fb.status"), DefaultKey.FB_STATUS);


        //Get Map in Set interface to get key and value
        Set s = mp.entrySet();

        return mp;
    }
}
